//
//  UIView+Extension.swift
//  AispeechMobileApp
//
//  Created by Lee on 2019/2/25.
//  Copyright © 2019年 Lee. All rights reserved.
//

import UIKit

extension UIView {
    
    func waterRippleAnimation(color:UIColor) {
       
        self.layer.backgroundColor = UIColor.clear.cgColor
        let pulseLayer = CAShapeLayer.init()
        pulseLayer.frame = self.layer.bounds
        pulseLayer.path = UIBezierPath.init(ovalIn: pulseLayer.bounds).cgPath
        pulseLayer.fillColor = color.cgColor
        pulseLayer.opacity = 0.0
        
        let replicatorLayer = CAReplicatorLayer.init()
        replicatorLayer.frame = self.bounds
        replicatorLayer.instanceCount = 4
        replicatorLayer.instanceDelay = 1
        replicatorLayer.addSublayer(pulseLayer)
        self.layer.addSublayer(replicatorLayer)
        
        let opacityAnima = CABasicAnimation.init(keyPath: "opacity")
        opacityAnima.fromValue = 1
        opacityAnima.toValue = 0.0
        
        let scaleAnima = CABasicAnimation.init(keyPath: "transform")
        scaleAnima.fromValue = NSValue.init(caTransform3D: CATransform3DScale(CATransform3DIdentity, 0.0, 0.0, 0.0))
        scaleAnima.toValue = NSValue.init(caTransform3D: CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 0.0))
        
        let groupAnima = CAAnimationGroup.init()
        groupAnima.animations = [opacityAnima,scaleAnima]
        groupAnima.duration = 4.0
        groupAnima.autoreverses = false
        groupAnima.repeatCount = HUGE
        groupAnima.isRemovedOnCompletion = false
        pulseLayer.add(groupAnima, forKey: "groupAnimation")
    }

    // MARK:设置阴影
    ///   - Parameters:
    ///   - width: 设置视图边框宽度
    ///   - bColor: 设置边框颜色
    ///   - sColor: 设置阴影颜色
    ///   - offset: 设置阴影偏移量
    ///   - opacity: 设置透明度
    ///   - radius: 设置阴影半径
    func setShadow(width:CGFloat,bColor:UIColor,
                   sColor:UIColor,offset:CGSize,opacity:Float,radius:CGFloat) {
        //设置视图边框宽度
        self.layer.borderWidth = width
        //设置边框颜色
        self.layer.borderColor = bColor.cgColor
        //设置边框圆角
        self.layer.cornerRadius = radius
        //设置阴影颜色
        self.layer.shadowColor = sColor.cgColor
        //设置透明度
        self.layer.shadowOpacity = opacity
        //设置阴影半径
        self.layer.shadowRadius = radius
        //设置阴影偏移量
        self.layer.shadowOffset = offset
        self.layer.masksToBounds = false
    }

    /// 部分圆角 /// /// - Parameters: /// - corners: 需要实现为圆角的角，可传入多个 /// - radii: 圆角半径
    func corner(byRoundingCorners corners: UIRectCorner, radii: CGFloat) {
        let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
    }
    
    //将当前视图转为UIImage
    func asImage() -> UIImage {
        let renderer = UIGraphicsImageRenderer(bounds: bounds)
        return renderer.image { rendererContext in
            layer.render(in: rendererContext.cgContext)
        }
    }
    
    // MARK: 简便切View的圆角
    func clicpCorner(cornorRadius: CGFloat)
    {
        self.layer.cornerRadius = cornorRadius
        self.layer.masksToBounds = true
    }
    
    // MARK:- 获取字符串的CGSize
    func getSize(font: UIFont, textWidth: CGFloat , contentStr: NSString) -> CGSize {
        let str = contentStr
        let size = CGSize(width: textWidth, height: CGFloat(MAXFLOAT))
        return str.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil).size
    }
    
    func getLabelWidth(labelStr:String,font:UIFont,height:CGFloat) -> CGFloat {
        
        let statusLabelText: NSString = labelStr as NSString
        let size = CGSize(width: 900, height: height)
        let strSize = statusLabelText.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil).size
        return strSize.width
    }

    //frame.origin.x
    public var n_left: CGFloat {
        get {
            return self.frame.origin.x
        }
        set {
            var frame = self.frame
            frame.origin.x = newValue
            self.frame = frame
        }
    }
    
    //frame.origin.y
    public var n_top: CGFloat {
        get {
            return self.frame.origin.y
        }
        set {
            var frame = self.frame
            frame.origin.y = newValue
            self.frame = frame
        }
    }
    
    //frame.origin.x + frame.size.width
    public var n_right: CGFloat {
        get {
            return self.frame.origin.x + self.frame.size.width
        }
        set {
            var frame = self.frame
            frame.origin.x = n_right - frame.size.width
            self.frame = frame
        }
    }
    
    //frame.origin.y + frame.size.height
    public var n_bottom: CGFloat {
        get {
            return self.frame.origin.y + self.frame.size.height
        }
        set {
            var frame = self.frame
            frame.origin.y = n_bottom - frame.origin.y
            self.frame = frame
        }
    }
    
    //frame.size.width
    public var n_width: CGFloat {
        get {
            return self.frame.size.width
        }
        set {
            var frame = self.frame
            frame.size.width = n_width
            self.frame = frame
        }
    }
    
    //frame.size.height
    public var n_height: CGFloat {
        get {
            return self.frame.size.height
        }
        set {
            var frame = self.frame
            frame.size.height = n_height
            self.frame = frame
        }
    }
    
    //center.x
    public var n_centerX: CGFloat {
        get {
            return self.center.x
        }
        set {
            self.center = CGPoint.init(x: n_centerX, y: self.center.y)
        }
    }
    
    //center.y
    public var n_center_Y: CGFloat {
        get {
            return self.center.y
        }
        set {
            self.center = CGPoint.init(x: self.center.x, y: n_center_Y)
        }
    }
    
    //frame.origin
    public var n_origin: CGPoint {
        get {
            return self.frame.origin
        }
        set {
            var frame = self.frame
            frame.origin = n_origin
            self.frame = frame
        }
    }
    
    //frame.size
    public var n_size: CGSize {
        get {
            return self.frame.size
        }
        set {
            var frame = self.frame
            frame.size = n_size
            self.frame = frame
        }
    }
    
    //maxX
    public var n_maxX: CGFloat {
        get {
            return self.frame.origin.x + self.frame.size.width
        }
    }
    
    //maxY
    public var n_maxY: CGFloat {
        get {
            return self.frame.origin.y + self.frame.size.height
        }
    }
    
}

// MARK: - Force Size
extension UIView {
    
    enum AttributeType {
        case width
        case height
        
        var attribute: NSLayoutConstraint.Attribute {
            switch self {
            case .width:  return .width
            case .height: return .height
            }
        }
    }
    
    
    func forceCompressedSize(type:AttributeType, constant: CGFloat) -> CGSize {
        let widthFenceConstraint = NSLayoutConstraint(item: self, attribute: type.attribute, relatedBy: .equal,
                                                      toItem: nil, attribute: .notAnAttribute, multiplier: 1.0,constant: constant)
        self.addConstraint(widthFenceConstraint)
        let size = self.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
        self.removeConstraint(widthFenceConstraint)
        return size
    }
    
}
